package com.ssyt.ec.stu.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;

import com.ssyt.ec.common.AppServlet;
import com.ssyt.ec.common.PageModel;
import com.ssyt.ec.common.util.FastjsonUtil;
import com.ssyt.ec.common.util.SysuserUtil;
import com.ssyt.ec.stu.entity.ExamInfoEnetity;
import com.ssyt.ec.stu.entity.PersonalEntity;
import com.ssyt.ec.stu.entity.StationEntity;
import com.ssyt.ec.stu.entity.ViewExamEntity;
import com.ssyt.ec.stu.service.StuService;
import com.ssyt.ec.sys.entity.SysUserEntity;

/**
 * Servlet implementation class StuServlet
 */
@WebServlet("/StuServlet")
public class StuServlet extends AppServlet {
	private static final long serialVersionUID = 1L;
	private StuService stuService = new StuService();

	public StuServlet() {
		super();
	}

	@Override
	protected void service(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String command = request.getParameter("command");
		if ("listStu".equals(command)) {
			listStu(request, response);
		} else if ("listExamInfo".equals(command)) {
			listExamInfo(request, response);

		} else if ("listStation".equals(command)) {
			listStation(request, response);
		} else if ("listStationMinute".equals(command)) {
			listStationMinute(request, response);
		} else if ("listPersonal".equals(command)) {
			listPersonal(request, response);
		}else if("listStuViewExam".equals(command)){
			listStuViewExam(request, response);
		}else if("listStuStationInfo".equals(command)){
			listStuStationInfo(request, response);
		}
	}

	/**
	 * 查看考试
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	protected void listStu(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		int page = Integer.parseInt(request.getParameter("page"));
		int rows = Integer.parseInt(request.getParameter("rows"));
		SysUserEntity user = (SysUserEntity) request.getSession().getAttribute(
				"sysUser");
		int userId = user.getId();
		int classId = user.getClassId();
		PageModel<ViewExamEntity> viewExamList = stuService.listStu(userId,
				classId, page, rows);
		print(response, FastjsonUtil.toJson(viewExamList));
	}

	/**
	 * 查看考试记录
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	protected void listExamInfo(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		int page = Integer.parseInt(request.getParameter("page"));
		int rows = Integer.parseInt(request.getParameter("rows"));
		SysUserEntity user = (SysUserEntity) request.getSession().getAttribute(
				"sysUser");
		int userId = user.getId();
		String paperName = request.getParameter("paperName");
		int isPass = 0;
		String isPassStr = request.getParameter("isPass");
		if (StringUtils.isNotBlank(isPassStr)) {
			isPass = Integer.parseInt(isPassStr);
		}
		PageModel<ExamInfoEnetity> examInfoList = stuService.listExamInfo(
				paperName, isPass, page, rows, userId);
		print(response, FastjsonUtil.toJson(examInfoList));
	}

	/**
	 * 查看内战信息
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	protected void listStation(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		int page = Integer.parseInt(request.getParameter("page"));
		int rows = Integer.parseInt(request.getParameter("rows"));
		SysUserEntity user = (SysUserEntity) request.getSession().getAttribute(
				"sysUser");
		int userId = user.getId();
		int classId = user.getClassId();
		PageModel<StationEntity> stationList = stuService.listStation(userId,
				classId, page, rows);
		print(response, FastjsonUtil.toJson(stationList));
	}

	/**
	 * 站内信息详细查看
	 *
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	protected void listStationMinute(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		int id = Integer.parseInt(request.getParameter("id"));
		StationEntity stationMinuteEntity = stuService.listStationMinute(id);
		print(response, FastjsonUtil.toJson(stationMinuteEntity));
	}

	/**
	 * 个人信息查询
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	protected void listPersonal(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		int page = Integer.parseInt(request.getParameter("page"));
		int rows = Integer.parseInt(request.getParameter("rows"));
		int userId = SysuserUtil.getUserId(request);
		PageModel<PersonalEntity> personalEntity = stuService.listPersonal(
				page, rows, userId);
		print(response, FastjsonUtil.toJson(personalEntity));
	}
	
	/**
	 * 学员代考查看主页面
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	protected void listStuViewExam(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		SysUserEntity user = (SysUserEntity) request.getSession().getAttribute(
				"sysUser");
		int userId = user.getId();
		int classId = user.getClassId();
		 ViewExamEntity viewExamEntity = stuService.listStuViewExam(userId,classId);
		 print(response, FastjsonUtil.toJson(viewExamEntity));
	}
	
	/**
	 * 主页站内信息查询
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	protected void listStuStationInfo(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		SysUserEntity user = (SysUserEntity) request.getSession().getAttribute(
				"sysUser");
		int userId = user.getId();
		int classId = user.getClassId();
		 StationEntity stationEntity = stuService.listStuStationInfo(userId,classId);
		 print(response, FastjsonUtil.toJson(stationEntity));
	}
}
